package com.cems.controller.level;

import com.cems.mapper.AdminMapper;
import com.cems.pojo.SysAdmin;
import com.cems.pojo.SysAdminInfo;
import com.cems.pojo.SysAdminUpgrade;
import com.cems.pojo.SysEcharts;
import com.cems.service.AdminService;
import com.cems.service.UserService;
import com.cems.util.LayuiReplay;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName levelController
 * @Author 陈新予(blank)
 * @Date 2021/5/16
 * @Version 1.0
 */
@Controller
public class levelController {
    @Autowired
    AdminService adminService;
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserService userService;
    @GetMapping("toadminlevel")
    public String toAdminLevel() {
        return "back/quanxian/quanxianlist";
    }
    @GetMapping("updateAdminAuthor")
    public void UpdateAdminLevel(HttpSession session,String id, String author, HttpServletResponse resp) {
        if (author.equals("最高管理员")) {
            author = "root";
        } else if (author.equals("超级管理员")) {
            author = "super";
        } else if (!author.equals("root") && !author.equals("super")) {
            author = "unfinished";
        }
        try {
            PrintWriter out = resp.getWriter();
            adminService.updateAdminLevel(id, author);
            adminMapper.GradeIsOK(Integer.parseInt(id));
            out.print("1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }
    @GetMapping("updateAdminDel")
    public void UpdateAdminStatus(HttpSession session,HttpServletResponse resp,String id,String adminStatus){
        try {
            PrintWriter out = resp.getWriter();
            if(adminStatus.equals("正常")){
                adminService.updateAdminStatus(id,"封禁");
            }else{
                adminService.updateAdminStatus(id,"正常");
            }
            out.print(1);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }

    @GetMapping("tolevelpage")
    public String toLevelUpPage() {
        return "back/quanxian/applicationform";
    }

    @GetMapping("addAdminUp")
    public String addAdminUP(String num, String target, String now, String desc) {
        int adminid = adminMapper.getAdminIdByNum(num);
        HashMap<String, String> map = new HashMap<>();
        map.put("adminId", String.valueOf(adminid));
        map.put("adminTarget", target);
        map.put("adminNow", now);
        map.put("adminReason", desc);
        adminService.addAdminUpgrade(map);
        return "back/quanxian/applicationform";
    }

    @GetMapping("getAdminUpgrade")
    @ResponseBody
    public Object getAllAdminUp(int page, int limit) {
        List<SysAdminUpgrade> adminUpgrade = adminService.getAdminUpgrade();
        List<SysAdminUpgrade> adminlimit = adminMapper.getAdminUpgradeLimit((page - 1) * limit, limit);
        return new LayuiReplay<SysAdminUpgrade>(0, "OK", adminUpgrade.size(), adminlimit);
    }

    @GetMapping("toadmingradePage")
    public String toAllAdminUpPage() {
        return "back/quanxian/adminUpgradeList";
    }

    @GetMapping("getadminReason")
    public void toReason(int id, HttpServletResponse resp) {
        String adminUpgrdeData = adminMapper.getAdminUpgrdeData(id);
        PrintWriter out = null;
        try {
            out = resp.getWriter();
            out.print(adminUpgrdeData);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("delAdmingrade")
    public void delgrade(int id, HttpServletResponse resp,HttpSession session) {
        try {
            PrintWriter out = resp.getWriter();
            adminMapper.delupgrade(id);
            out.print("1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            SysAdmin admin = (SysAdmin) session.getAttribute("loginAdmin");
            SysEcharts sys = userService.selOneSysSuc(admin.getId());
            Map<String,Object> map = new HashMap<>();
            map.put("adminId",admin.getId());
            map.put("operateNum",sys.getOperateNum()+1);
            userService.setSysLoginGraph(map);
        }

    }
}
